package com.ruicar.afs.cloud.bizcommon.rules.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.bizcommon.enums.rules.RuleOperateType;
import com.ruicar.afs.cloud.bizcommon.rules.entity.AfsRuleOperateHis;
import com.ruicar.afs.cloud.bizcommon.rules.mapper.AfsRuleOperateHisMapper;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleOperateHisService;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * AfsRuleOperateHisServiceImpl
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date create on 2020-05-16 09:38:03
 * @since 1.0
 */
@Service
@Slf4j
public class AfsRuleOperateHisServiceImpl extends ServiceImpl<AfsRuleOperateHisMapper, AfsRuleOperateHis> implements AfsRuleOperateHisService {
    /**
     * Create rule *
     *
     * @param ruleId rule id
     */
    @Override
    public void createRule(Long ruleId) {
        AfsRuleOperateHis afsRuleOperateHis = new AfsRuleOperateHis();
        afsRuleOperateHis.setHisType(RuleOperateType.CREATE);
        ;
        afsRuleOperateHis.setOperateDate(new Date());
        afsRuleOperateHis.setOperator(SecurityUtils.getUsernameWithDefault("system"));
        afsRuleOperateHis.setTargetRuleId(ruleId);
        this.save(afsRuleOperateHis);
    }

    /**
     * Modify rule *
     *
     * @param oldRuleId old rule id
     * @param newRuleID new rule id
     * @param type      type
     */
    @Override
    public void modifyRule(Long oldRuleId, Long newRuleID, RuleOperateType type) {
        AfsRuleOperateHis afsRuleOperateHis = new AfsRuleOperateHis();
        afsRuleOperateHis.setSourceRuleId(oldRuleId);
        afsRuleOperateHis.setHisType(type);
        afsRuleOperateHis.setOperateDate(new Date());
        afsRuleOperateHis.setOperator(SecurityUtils.getUsernameWithDefault("system"));
        afsRuleOperateHis.setTargetRuleId(newRuleID);
        this.save(afsRuleOperateHis);
    }


    /**
     * New version *
     *
     * @param oldRuleId old rule id
     * @param newRuleID new rule id
     */
    @Override
    public void newVersion(Long oldRuleId, Long newRuleID) {
        AfsRuleOperateHis afsRuleOperateHis = new AfsRuleOperateHis();
        afsRuleOperateHis.setSourceRuleId(oldRuleId);
        afsRuleOperateHis.setHisType(RuleOperateType.NEW_VERSION);
        ;
        afsRuleOperateHis.setOperateDate(new Date());
        afsRuleOperateHis.setOperator(SecurityUtils.getUsernameWithDefault("system"));
        afsRuleOperateHis.setTargetRuleId(newRuleID);
        this.save(afsRuleOperateHis);
    }

    /**
     * Active rule *
     *
     * @param ruleId rule id
     */
    @Override
    public void activeRule(Long ruleId) {
        AfsRuleOperateHis afsRuleOperateHis = new AfsRuleOperateHis();
        afsRuleOperateHis.setHisType(RuleOperateType.ACTIVE);
        ;
        afsRuleOperateHis.setOperateDate(new Date());
        afsRuleOperateHis.setOperator(SecurityUtils.getUsernameWithDefault("system"));
        afsRuleOperateHis.setTargetRuleId(ruleId);
        this.save(afsRuleOperateHis);
    }

    /**
     * Expired rule *
     *
     * @param ruleId rule id
     */
    @Override
    public void expiredRule(Long ruleId) {
        AfsRuleOperateHis afsRuleOperateHis = new AfsRuleOperateHis();
        afsRuleOperateHis.setHisType(RuleOperateType.EXPIRED);
        ;
        afsRuleOperateHis.setOperateDate(new Date());
        afsRuleOperateHis.setOperator(SecurityUtils.getUsernameWithDefault("system"));
        afsRuleOperateHis.setTargetRuleId(ruleId);
        this.save(afsRuleOperateHis);
    }
}
